<?php

include_once '../control/conexao.php';
include_once 'menu.class.php';

class menuDAO {

    public function listar_por_perfil($perfil) {

        $consulta = mysql_query("SELECT * FROM menu WHERE id_menu IN (SELECT menu_id_menu FROM perfil_menu WHERE perfil_id_perfil='$perfil')");
        $ln = mysql_num_rows($consulta);
        $arMenu = array();
        for ($i = 0; $i < $ln; $i++) {
            $arMenu[$i] = new menu();
            $arMenu[$i]->id_menu = mysql_result($consulta, $i, 'id_menu');
            $arMenu[$i]->menu_nome = mysql_result($consulta, $i, 'menu_nome');
            $arMenu[$i]->menu_icone = mysql_result($consulta, $i, 'menu_icone');
            $arMenu[$i]->menu_link = mysql_result($consulta, $i, 'menu_link');
        }
        return $arMenu;
    }

    public function listar() {

        $consulta = mysql_query("SELECT * FROM menu");
        $ln = mysql_num_rows($consulta);
        $arMenu = array();
        for ($i = 0; $i < $ln; $i++) {
            $arMenu[$i] = new menu();
            $arMenu[$i]->id_menu = mysql_result($consulta, $i, 'id_menu');
            $arMenu[$i]->menu_nome = mysql_result($consulta, $i, 'menu_nome');
            $arMenu[$i]->menu_icone = mysql_result($consulta, $i, 'menu_icone');
            $arMenu[$i]->menu_link = mysql_result($consulta, $i, 'menu_link');
        }
        return $arMenu;
    }

    public function listar_por_submenu($id) {
        $consulta = mysql_query("SELECT menu_nome FROM menu WHERE id_menu = '$id'");
        $menu = new menu();
        $menu->menu_nome = mysql_result($consulta, 0, 'menu_nome');
        return $menu;
    }

    public function consultar($id) {
        $consulta = mysql_query("SELECT * FROM menu WHERE id_menu = '$id'");
        $menu = new menu();
        $menu->id_menu = mysql_result($consulta, 0, 'id_menu');
        $menu->menu_nome = mysql_result($consulta, 0, 'menu_nome');
        $menu->menu_link = mysql_result($consulta, 0, 'menu_link');
        $menu->menu_icone = mysql_result($consulta, 0, 'menu_icone');
        return $menu;
    }

    public function alterar($menu) {
        $altera = mysql_query("UPDATE menu SET menu_nome='$menu->menu_nome', menu_icone ='$menu->menu_icone', menu_link='$menu->menu_link' WHERE id_menu = '$menu->id_menu'");
        if ($altera == true) {
            echo "<script> alert ('Menu alterado com sucesso!') </script>";
            echo "<script> history.go (-2) </script>";
        } else {
            echo "<script> alert ('Servidor com problemas, tente mais tarde') </script>";
            echo "<script> history.go (-2) </script>";
        }
    }

    public function insere($menu) {
        $insere = mysql_query("INSERT INTO menu (menu_nome, menu_icone, menu_link) VALUES ('$menu->menu_nome','$menu->menu_icone','$menu->menu_link')");
        if ($insere == true) {
            echo "<script> alert ('Menu inserido com sucesso!') </script>";
            echo "<script> history.go (-2) </script>";
        } else {
            echo "<script> alert ('Servidor com problemas, tente mais tarde') </script>";
            echo "<script> history.go (-2) </script>";
        }
    }
    
    public function excluir($menu) {
        $exclui = mysql_query("DELETE FROM menu WHERE id_menu='$menu->id_menu'");
        if ($exclui == true) {
            echo "<script>alert('Menu excluido com sucesso!')</script>";
            echo "<script>history.go(-1)</script>";
        } else {
            echo "<script>alert('Erro! Servidor com problemas, tente mais tarde')</script>";
            echo "<script>history.go(-1)</script>";
        }
    }
    
}
